package com.mj.mes.system.SysLogger;

import org.apache.commons.collections.map.HashedMap;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * Created by Cyrann on 2017/8/21.
 */
@Controller
@RequestMapping("logger")
public class SysLoggerController {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private SysLoggerService sysLoggerService;

//    @Autowired


    /**
     */
    @PostMapping(value = "queryWithPaging")
    @ResponseBody
    public Map<String, Object> findAll(@RequestParam(value = "fieldName", required = false, defaultValue = "") String s_deviceName,
                                        Integer page,
                                        @RequestParam(value = "rows") Integer size) {
        Map<String, Object> map = new HashedMap();
        int total;
        Page<SysLogger> list;
        if (s_deviceName.equals("")) {

            list = sysLoggerService.findAll(new PageRequest(page - 1, size));
            total = sysLoggerService.findAll().size();
        } else {
            list = sysLoggerService.findByUserName(s_deviceName, new PageRequest(page - 1, size));
            total = sysLoggerService.findByUserName(s_deviceName, new PageRequest(page - 1, size)).getSize();
        }
        map.put("total", total);
        map.put("rows", list.getContent());
        return map;
    }

    /**
     *
     */
    @RequestMapping(value = "add", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> add(SysLogger sysLogger) {
        Map<String, Object> map = new HashedMap();
        //默认状态为1(空闲)
        try {
            sysLoggerService.addSysLogger(sysLogger);
            map.put("msg", "success");
        } catch (Exception e) {
            map.put("msg", "fail");
            logger.error(e.getMessage());
        }
        return map;
    }

    @RequestMapping(value = "sessionTest",method = RequestMethod.GET)
    @ResponseBody
    public String sessionTest(){
        SecurityUtils.getSubject().getPrincipal();
        return (String) SecurityUtils.getSubject().getPrincipal();
    }
}
